package sagroup.dracots;

import lights.adapters.Tuple;
import lights.interfaces.ITuple;
import lime2.Location;
import sagroup.dracots.SystemTupleType;
import sagroup.dracots.TupleType;

//@SuppressWarnings("serial")
public class FlatSignalTuple extends SignalTuple {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6262773475545613870L;
	public ITuple t;
	
	public FlatSignalTuple() {
		//empty constructor
	}

	public FlatSignalTuple(ITuple t) {
		this(Location.HERE, t);
	}
	
	public FlatSignalTuple(Location dest, ITuple t) {
		this(SystemTupleType.ANYTYPE, dest, t);
	}

	public FlatSignalTuple(TupleType tupleType, Location dest, ITuple t) {
		super(tupleType, dest);
		this.t = t;
	}
	
	public FlatSignalTuple(TupleType tupleType, ITuple t) {
		this(tupleType, Location.HERE, t);
	}
	
	public static FlatSignalTuple createMatchAllTemplate() {
		return (FlatSignalTuple)createMatchAllTemplateByClass(FlatSignalTuple.class);
	}
	
	/**
	 */
	public ITuple getTuple() {
		return t;
	}

	/**
	 * @param t the tuple to set
	 */
	public void setTuple(ITuple t) {
		this.t = t;
	}

	@Override
	protected void customCreateFromFlat(ITuple flat) {
		int start = getAllClzFieldsForTuple(this.getClass()).size() + DracoConstants.DRACO_TAGS_NUMBER;
		int end = flat.length();
		if(isMatchAll) end--;
		ITuple t = new Tuple();
		for(int i = start; i < end; i++) {
			t.insertAt(flat.get(i), i - start);
		}
		this.t = t;
	}
	
	@Override
	protected void customFlatten(ITuple flat) {
		if(t != null) {
			int start = flat.length();
			for(int i = 0; i < t.length(); i++) {
				flat.insertAt(t.get(i), i + start);
			}
		}
	}
}
